package view.toolboxComponents;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import controller.CustomEvent;
import controller.EventHandler;
import controller.ToolBoxEventHandler;
/** * A class to encapsulate the name panel for the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
@SuppressWarnings("serial")
public class PrescriptionNamePanel extends AbstractPanel{
	private JLabel lblPresName;
	private JTextField txtPresName;
	private EventHandler eventHandler;
	public PrescriptionNamePanel(){
		super();
		txtPresName = new JTextField();
		lblPresName = new JLabel("Name");
		txtPresName.setMaximumSize(new Dimension(120, 20));
		//When enter is pressed in this textBox the name of the prescription changes
		txtPresName.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					CustomEvent event = new CustomEvent("presNameChange",this, txtPresName.getText());
					PrescriptionNamePanel.this.eventHandler = new ToolBoxEventHandler();
					PrescriptionNamePanel.this.eventHandler.createEvent(event);
				}
			}
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent e) {
			}
		});
		buildPanel();
	}
	/**
	 * Set the text in the textfield to specified text
	 * @param text the text to set as the name
	 */
	public void setText(String text){
		txtPresName.setText(text);
	}
	/**
	 * Build prescription name panel
	 */
	private void buildPanel() {
		this.add(lblPresName);
		makeRigidArea();
		this.add(txtPresName);
	}	@Override
	public void resetComponent(){
		txtPresName.setText("");
	}
}
